package com.yangtzeu.jwc.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.yangtzeu.jwc.fragment.IndexFragment;
import com.yangtzeu.jwc.utils.G;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.L;
import com.zjsx.lib.utils.T;

/**
 * 显示详细的新闻
 */
public class NewsActivity extends BaseActivity {
    private WebView webView;
    private String title,content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    }

    @Override
    public void setView() {
	setContentView(R.layout.activity_news);
    }

    @Override
    public void initData() {
	super.initData();
	Intent intent = getIntent();
	title = intent.getStringExtra("title");
	content = intent.getStringExtra("content");
    }

    @Override
    public void initActionBar() {
	setSimpleActionBar(null, title, null, null);
    }

    @Override
    public void initView() {
	super.initView();
	webView = (WebView) findViewById(R.id.webView_news);
	webView.setHorizontalScrollBarEnabled(false);
	webView.setVerticalScrollBarEnabled(false);
	webView.setWebViewClient(new MyWebViewClient());
	webView.loadDataWithBaseURL(G.NEW_JWC_HOME, content, "text/html", "utf-8", null);
    }

    /*
     * 自定义点击链接后的打开方式
     */
    private class MyWebViewClient extends WebViewClient {
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
	    //T.showShort(NewsActivity.this, url);
	    Intent intent = null;
	    // 没有http头,添加上教务处的网址
	    if (!url.startsWith("http://") && !url.startsWith("mailto")) {
		url = G.NEW_JWC_HOME + url;
	    } else if (url.startsWith(G.YANGTZEU_JWC_INNER_PATH)) {
		// 如果是内网,尝试转化为外网
		L.d("检测到内网地址!");
		url = url.replace(G.YANGTZEU_JWC_INNER_PATH, G.NEW_JWC_HOME);
	    }
	    intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
	    // L.d("进入网址:"+url);
	    try {
		startActivity(intent);
	    } catch (Exception e) {
		Toast.makeText(getApplicationContext(), "无法解析该网址", Toast.LENGTH_LONG).show();
	    }
	    return true;
	}
    }
}
